<?php

    /**
     * Json
     *
     * Handle a Json call.
     * The response has to be a Json response.
     *
     * @package      Core
     * @subpackage   Controller_Request
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Core_Controller_Request_Json extends Core_Controller_Request_Abstract
    {

    ////////////////////////////////////////////////////////////////////////////
	//                                Variables                               //
	////////////////////////////////////////////////////////////////////////////

		/**
		 * The identifier of the JSON request
		 *
		 * @var string
		 */
		protected $_jsonRequestId = null;

        /**
         * The request type code
         *
         * @var string
         */
        protected $_type = 'json';

        /**
         * The type of the default response
         *
         * @var string
         */
        protected $_defaultResponseType = 'json';

	////////////////////////////////////////////////////////////////////////////
	//                            Getters / Setters                           //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Gets the identifier of the JSON request
         *
         * @return      string
         */
        public function getJsonRequestId()
        {
            return $this->_jsonRequestId;
        }

        

    ////////////////////////////////////////////////////////////////////////////
	//                            Protected methods                           //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Initializes the parameters of the request
         *
         * @return      void
         */
        protected function _initParams()
        {
			global $HTTP_RAW_POST_DATA;
            $request        = '';
            $requestobj     = array();

			if (isset($HTTP_RAW_POST_DATA)) {
				$request = $HTTP_RAW_POST_DATA;
            } else {
				$request = Core_IO_File::readAllLines('php://input');
				$request = implode("\n", $request);
			}

			// Decodes the request
            $requestobj = (array) explode('&', $request);
            $module     = '';
            $action     = '';

            $this->_params = array();

            foreach($requestobj as $params) {
                $param = explode('=', $params);
                if ($param[0] == 'method') {
                   list($module, $action) = explode('~', $param[1]);

                } elseif ($param[0] == 'requestId') {
                    $this->_jsonRequestId = $param[1];

                } elseif (isset($param[1])) {
                    $this->_params[$param[0]] = urldecode($param[1]);
                }
            }

			// Define the action to execute
			$this->_params['module'] = $module;
			$this->_params['action'] = urldecode($action);
        }

		/**
		 * Returns a list of class name of allowed response corresponding to the request
		 *
		 * @return      array
		 */
		protected function _allowedResponses()
		{
			return array('Core_Controller_Response_Json');
		}
    }
